﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Text.RegularExpressions;

namespace DreametryMIS
{
    
    public partial class GraphicDesigners : Form
    {
        
        SqlConnection connection;

        //-------------------------------
        List<Panel> pnl = new List<Panel>();
        List<Label> tasklevel = new List<Label>();
        List<Label> taskname = new List<Label>(0);
        //--------------------------------

        public GraphicDesigners()
        {
            InitializeComponent();
         
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            pnlLoading.BringToFront();
            this.webBrowser2.Navigate("www.google.com");

            lblName.Text = ClassFlow.global_fullname;
            btnChat.PerformClick();
            panel2.Height = 0;
           
            ClassFlow.global_count = "SELECT Count(*) FROM EmploeeTask WHERE emp_id = '" + ClassFlow.global_id + "' AND flow_id < 8";
            ClassFlow.unique_SQL_String = "SELECT * FROM EmploeeTask WHERE emp_id =  '" + ClassFlow.global_id + "' AND flow_id < 8";

            this.flowControl1.getAll();

            taskInProgress.Path = @"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\" + ClassFlow.global_nickname+ @"\logs\comment";

            //get store data
            var fileStream = new FileStream(@"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Jay\logs\Res\str.txt", FileMode.Open, FileAccess.Read);
            using (var streamReader = new StreamReader(fileStream, Encoding.Default))
            {
                string line;
                while ((line = streamReader.ReadLine()) != null)
                {
                    cmbStore.Items.Add(line);
                }
            }
        }

        private void btnCodiga_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://codiga.basecamphq.com/login");
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            this.Close();
            MainForm frmMain = new MainForm();
            frmMain.Show();
        }

        private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            pnlLoading.SendToBack();
        }

        private void btnTrello_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://trello.com/b/49QwUeTv/dreametry-team-tasks");
        }

        private void btnillustrator_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(@"C:\Program Files\Adobe\Adobe Illustrator CC 2015\Support Files\Contents\Windows\Illustrator.exe");
            }
            catch
            {
                MessageBox.Show("Path not found");
            }
        }

        private void btnPhotoshop_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(@"C:\Program Files\Adobe\Adobe Photoshop CC 2015\Photoshop.exe");
            }
            catch
            {
                MessageBox.Show("Path not found");
            }
        }

        private void btnMyFolder_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(@"\\SERVER-PC\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\"+ClassFlow.global_nickname);
            }
            catch
            {
                MessageBox.Show("Path not found");
            }
        }

        private void btnChrome_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("www.google.com");
        }

        private void btnSearchFiles_Click(object sender, EventArgs e)
        {
            btnSearchFiles.BackColor = Color.PaleVioletRed;
            pnlSearch.BringToFront();
            txtSearchKeyword.Focus();
        }

        private void btnChat_Click(object sender, EventArgs e)
        {
            if(btnChat.Text == "Hide Chat")
            {
                pnlTlk.Width = 0;
                btnChat.Text = "Show Chat";
            }
            else
            {
                pnlTlk.Width = 357;
                btnChat.Text = "Hide Chat";
            }
        }

        private void btnCreateTask_Click(object sender, EventArgs e)
        {
            if (txtTaskName.Text.Contains("'") || txtTaskName.Text.Contains("http://") || txtTaskName.Text.Contains("www.") || txtTaskName.Text.Contains(".com"))
            {
                MessageBox.Show("Please remove (') in the title or the title has a link", "Wrong Taskname", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                String desc;
                String rawDesc;


                rawDesc = txtDescription.Text;
                String finaDesc = rawDesc.Replace(Environment.NewLine, "<br>");
                desc = "<a href=" + txtCodigaLink.Text.Replace("https://", "") + ">" + cmbStore.Text + "</a><br><br>" + finaDesc;

                String x;

                if (txtTaskName.Text == "" || txtDescription.Text == "" || cmbStore.Text == "")
                {
                    MessageBox.Show("Please fill all the fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    String dataCount = "SELECT Count(*) FROM TaskTable WHERE task_name ='"+ txtTaskName.Text + "'";
                    using (connection = new SqlConnection(ClassFlow.global_con))
                    {
                        SqlCommand command = new SqlCommand(dataCount, connection);
                        command.Connection.Open();
                        x = command.ExecuteScalar().ToString();
                    }
                    if (x == "0")
                    {

                        String addData;
                    
                        if (cmbCategory.Text == "Design")
                        {
                            addData = "INSERT INTO TaskTable (task_name, task_description, date, flow_id, emp_id) VALUES(@name, @desc, @date, 1, " + ClassFlow.global_id + ")";
                        }
                        else
                        {
                            addData = "INSERT INTO TaskTable (task_name, task_description, date, flow_id, emp_id) VALUES(@name, @desc, @date, 7, " + ClassFlow.global_id + ")";
                        }
                    
                        using (connection = new SqlConnection(ClassFlow.global_con))
                        {
                            SqlCommand command = new SqlCommand(addData, connection);
                            command.CommandType = CommandType.Text;
                            command.Parameters.AddWithValue("@name", txtTaskName.Text);
                            command.Parameters.AddWithValue("@desc", desc);
                            command.Parameters.AddWithValue("@date", DateTime.Now);
                    
                            command.Connection.Open();
                            command.ExecuteNonQuery();
                            MessageBox.Show("Task has been created", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            command.Connection.Close();
                            this.flowControl1.getAll();
                        }
                        //writeFile.Main(DateTime.Now + " Task name: " + txtTaskName.Text + " - updated by " + ClassFlow.global_fullname + " - Designer (Task Created)", @"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Deo\C#\Backup Files\Designers Logs\logs.txt");
                    
                        writeFile.Main(txtCodigaLink.Text + Environment.NewLine + Environment.NewLine + ClassFlow.line + Environment.NewLine + Environment.NewLine + "Date: " + DateTime.Now + Environment.NewLine + "Task name: " + txtTaskName.Text + Environment.NewLine + "Designer: " + ClassFlow.global_fullname + "Task Created" + Environment.NewLine + Environment.NewLine + ClassFlow.line + Environment.NewLine, @"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\" + ClassFlow.global_nickname + @"\logs\" + txtTaskName.Text + @".txt");
                        writeFile.Main("Date: " + DateTime.Now + Environment.NewLine + "Task name: " + txtTaskName.Text + Environment.NewLine + "Designer: " + ClassFlow.global_fullname + "Task Created", @"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Deo\C#\Backup Files\Verify Logs\logs.txt");
                        txtTaskName.Clear();
                        txtCodigaLink.Clear();
                        txtDescription.Clear();
                        btnCreateTaskCancel.PerformClick();
                    }
                    else
                    {
                        MessageBox.Show("Task already exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void btnCreateTaskCancel_Click(object sender, EventArgs e)
        {
            panel2.Height = 0;
            btnCreatetsk.Visible = true;
        }

        private void btnCreatetsk_Click(object sender, EventArgs e)
        {
            panel2.Height = 248;
            btnCreatetsk.Visible = false;
        }

        private void btnJpeg_Click_1(object sender, EventArgs e)
        {
            try
            {
                string folder = Uri.EscapeDataString(@"\\SERVER-PC\Dropbox");
                string file = txtSearchSupp.Text + @" System.FileName:~=""" + txtSearchKeyword.Text + @""" type:jpg";
                string uri = "search:query=" + file + "&crumb=location:" + folder;

                Process.Start(new ProcessStartInfo(uri));
            }
            catch
            {
                MessageBox.Show("Search path not found");
            }
        }

        private void btnPng_Click(object sender, EventArgs e)
        {
            try
            {
                string folder = Uri.EscapeDataString(@"\\SERVER-PC\Dropbox");
                string file = txtSearchSupp.Text + @" System.FileName:~=""" + txtSearchKeyword.Text + @""" type:png";
                string uri = "search:query=" + file + "&crumb=location:" + folder;

                Process.Start(new ProcessStartInfo(uri));
            }
            catch
            {
                MessageBox.Show("Search path not found");
            }
        }

        private void btnPsd_Click(object sender, EventArgs e)
        {
            try
            {
                string folder = Uri.EscapeDataString(@"\\SERVER-PC\Dropbox");
                string file = txtSearchSupp.Text + @" System.FileName:~=""" + txtSearchKeyword.Text + @""" type:psd";
                string uri = "search:query=" + file + "&crumb=location:" + folder;

                Process.Start(new ProcessStartInfo(uri));
            }
            catch
            {
                MessageBox.Show("Search path not found");
            }
        }

        private void btnAi_Click(object sender, EventArgs e)
        {
            try
            {
                string folder = Uri.EscapeDataString(@"\\SERVER-PC\Dropbox");
                string file = txtSearchSupp.Text + @" System.FileName:~=""" + txtSearchKeyword.Text + @""" type:ai";
                string uri = "search:query=" + file + "&crumb=location:" + folder;

                Process.Start(new ProcessStartInfo(uri));
            }
            catch
            {
                MessageBox.Show("Search path not found");
            }
        }

        private void btnClosePnlSrc_Click(object sender, EventArgs e)
        {
            btnSearchFiles.BackColor = Color.WhiteSmoke;
            pnlSearch.SendToBack();
        }

        private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
            this.flowControl1.getAll();
            notifyIcon1.ShowBalloonTip(5000,"","New notification for you",ToolTipIcon.Info);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            
            this.flowControl1.getAll();
        }

        private void taskInProgress_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            String path = e.FullPath;
            String result = System.IO.Path.GetFileName(path);
            String y = Path.GetDirectoryName(result.TrimEnd(Path.DirectorySeparatorChar));
            String x = Path.GetFileNameWithoutExtension(result);

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            // MessageBox.Show(x);

            notifyIcon1.ShowBalloonTip(10000, "Comment on your task", x, ToolTipIcon.Info);
        }

        private void GraphicDesigners_Activated(object sender, EventArgs e)
        {

        }

        private void taskInProgress_Deleted(object sender, FileSystemEventArgs e)
        {
            String path = e.FullPath;
            String result = System.IO.Path.GetFileName(path);
            String y = Path.GetDirectoryName(result.TrimEnd(Path.DirectorySeparatorChar));
            String x = Path.GetFileNameWithoutExtension(result);

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            // MessageBox.Show(x);

            notifyIcon1.ShowBalloonTip(10000, "Comment on your task", x, ToolTipIcon.Info);
        }
    }
}
